package com.baijiayun.sdk.core;

import com.baijiayun.sdk.exception.ApiException;
import com.baijiayun.sdk.model.GetCategoryListRequest;
import com.baijiayun.sdk.model.GetCategoryListResponse;
import com.baijiayun.sdk.service.VideoService;

import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * 服务工厂
 */
public class ServiceFactory extends CommonService {

    /**
     * @throws ApiException
     */
    public ServiceFactory() throws ApiException {
        this("/baijiayun-api.properties");
    }

    /**
     * @param propertiesFileName
     * @throws ApiException
     */
    public ServiceFactory(String propertiesFileName) throws ApiException {
        super();
        Properties props = new Properties();
        try {
            props.load(ServiceFactory.class.getResourceAsStream(propertiesFileName));
            readConfig(props);
        } catch (Exception ioe) {
            logger.error("Cannot access the config file [" + propertiesFileName + "].", ioe);
        }
        logger.info("Instantiate properties for [" + propertiesFileName + "] was successful.");
    }

    public ServiceFactory(String domainName, int partnerId, String partnerKey, String secretKey) throws ApiException {
        super();
        try {
            this.domainName = domainName;
            this.partnerId = partnerId;
            this.partnerKey = partnerKey;
            this.secretKey = secretKey;
        } catch (Exception ioe) {

        }
    }

    /**
     * @return
     * @throws ApiException
     */
    public static ServiceFactory getInstance() throws ApiException {
        return new ServiceFactory();
    }

    /**
     * @param propertiesFileName
     * @return ServiceFactory
     * @throws ApiException
     */
    public static ServiceFactory getInstance(String propertiesFileName) throws ApiException {
        return new ServiceFactory(propertiesFileName);
    }


    /**
     * @param domainName
     * @param partnerId
     * @param partnerKey
     * @param secretKey
     * @return
     * @throws ApiException
     */
    public static ServiceFactory getInstance(String domainName, int partnerId, String partnerKey, String secretKey) throws ApiException {
        return new ServiceFactory(domainName, partnerId, partnerKey, secretKey);
    }

    /**
     * 读取配置
     *
     * @param props
     */
    private void readConfig(Properties props) {
        for (Map.Entry<Object, Object> entry : props.entrySet()) {
            String key = entry.getKey().toString();
            String value = entry.getValue().toString();
            super.setConfig(key, value);
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) throws IllegalAccessException {
        try {
            String domainName = "i83315257.at.baijiayun.com";
            int partnerId = 83315257;
            String partnerKey = "V8PNRaLoofGw5E5uU1muW1AE3WHgtEN6i2IX+WkELxJ7o0MIj/fwSHqiAOFV2U1bL7WqT8tTIJk9sIX1MD3foEK4rA+ov1WCttHyW+xUMdOTlP1ULNh6vttYCeUJPADt";
            String secretKey = "d928294938a3f09ed41b09a134699566";
            ServiceFactory serviceFactory = ServiceFactory.getInstance(domainName, partnerId, partnerKey, secretKey);
            VideoService videoService = serviceFactory.getJsonService(VideoService.class);
            GetCategoryListRequest request = new GetCategoryListRequest();
            request.setPartnerId(partnerId);
            Msg<GetCategoryListResponse> response = videoService.getCategoryList(request);
            List<GetCategoryListResponse.Category> aa = response.getData().getList();
            String a = "";
        } catch (ApiException e) {
            e.printStackTrace();
        }
    }
}
